<?php

namespace Apps\AppApi\Method;


use Apps\Common\Enum\PayType;
use Apps\Common\Libs\UserCommon;
use Apps\Common\Models\BeautyParlorTag;
use Apps\Common\Models\SysOpenCity;
use Apps\Common\Models\UserBase;
use Apps\Common\Models\UserPushRelation;
use PhalconPlus\Api\Common\HandleApiBase;
use PhalconPlus\Api\Common\IHandleApi;
use Phalcon\Config;

class BaseMethod extends HandleApiBase implements IHandleApi
{ 
	
    /**
     * 获取开通城市的ID
     * @param $code
     * @return int
     */
    public function getCityId($code) {
        $city = SysOpenCity::query()
            ->columns('city_id')
            ->where('city_code=:code: and city_state = 1')
            ->bind(['code'=>$code])
            ->limit(1)
            ->execute()->getFirst();
        return $city ? $city->city_id : 0;
    }

    /**
     * 获取一个开通城市的信息
     * @param $code
     * @return mixed
     */
    public function getCity($code)
    {
        $data = SysOpenCity::find(['conditions' => 'city_code=' . $code . ' and city_state = 1'])->toArray();
        return $data ? $data[0] : [];
    }

    /**
     * 获取美容院的标签信息
     * @param $id
     * @return array
     */
    public function getBeautyParlorTags($id)
    {
        $data = BeautyParlorTag::query()
            ->columns([
                'tag_title'
            ])
            ->leftJoin('Apps\Common\Models\BeautyParlorTagInfo', 'ti.tag_id = Apps\Common\Models\BeautyParlorTag.tag_id', 'ti')
            ->where('bp_id = :id: AND tag_state > 0', ['id'=>$id])
            ->execute();
        $result = [];
        foreach ($data as $d) {
            $result[] = $d->tag_title;
        }

        return $result;
    }

    /**
     * 获取支付方式的值
     * @param $value
     * @return int
     */
    public function getPayTypeNum($value)
    {
        return PayType::getPayTypeNum($value);
    }

    /**
     * 获取支付的字符串形式
     * @param $value
     * @return string
     */
    public function getPayTypeStr($value)
    {
        $payTypeStr = '';
        switch($value) {
            case 1:
                $payTypeStr = 'weixin';
                break;
            case 2:
                $payTypeStr = 'alipay';
                break;
        }
        return $payTypeStr;
    }

    /**
     * 密码加密
     * @param string $pwd
     * @return string
     */
    public function hashPassword($pwd)
    {
        return UserCommon::hashPassword($pwd, $this->getDi());
    }

    /**
     * 保存用户push id
     * @param $userId
     * @param $pushId
     * @param $osType
     */
    public function saveUserPushId($userId, $pushId, $osType)
    {
        $pushType = 1;
        $osType = strtolower($osType) == 'ios' ? 2 : 1;
        // 是否存在
        $exist = UserPushRelation::query()
            ->where('push_type = :ptype: AND os_type = :otype: AND user_id = :uid: AND push_relation_state = 1 AND push_id = :pid:')
            ->bind([
                'ptype' => $pushType,
                'otype' => $osType,
                'uid' => $userId,
                'pid' => $pushId
            ])
            ->execute()
            ->getFirst();
        if($exist) {
            return;
        }
        $upr = new UserPushRelation();
        $upr->push_type = $pushType;
        $upr->user_id = $userId;
        $upr->os_type = $osType;
        $upr->push_id = $pushId;
        if($upr->save() == false) {
            $this->databaseErrorLog($upr);
        }
    }

    /**
     * 通过用户账号获取用户信息
     * @param $account
     * @return UserBase
     */
    public function userInfoByAccount($account)
    {
        //return UserBase::findFirst("user_account='".$account."'");
        return UserBase::findFirst([
            'conditions' => 'user_account = :account:',
            'bind' => [
                'account' => $account
            ]
        ]);
    }

    /**
     * 正则获取字符串长度
     * @param null $string
     * @return int
     */
    public function utf8_strlen($string = null)
    {
        // 将字符串分解为单元
        preg_match_all("/./us", $string, $match);
        // 返回单元个数
        return count($match[0]);
    }
}